<?php
namespace app\admin\controller\search;

use app\common\model\InStore as Model;
use app\common\model\Store;
use app\common\model\Goods;

use app\admin\classes\BasicSearch;
use core\Api;
use core\V;
use core\Res;

/**
 *
 */


class InStore extends BasicSearch {

  static $model=Model::class;
  static $download=false;

  // static $hash="/admin/goods/goods";

  static $title=[
    "show"=>true,
    "title"=>"入库记录查询",
    "breadcrumb"=>["查询","库存查询","入库记录"],
    "subtitle"=>"查询入库记录信息",
  ];
  protected function before_search($form){
    $form['status']=2;
    return $form;
  }
  protected function after_search(&$query){
    $query->with("goods,store");
  }
  protected function before_result(&$query){
    $query->hidden(["update_at","delete_at"]);
    $query->order(["create_at"=>"desc"]);
  }
  protected function make_list(&$query){
    $res=$query->all();
    $list=[];
    foreach ($res as $v) {
      $list[]=[
        "id"=>$v->id,
        "goods_code"=>$v->goods->code,
        "goods_name"=>$v->goods->name,
        // "store_code"=>$v->store->code,
        "store_name"=>$v->store->name,
        "date"=>$v->create_at,
        "num"=>$v->num,
      ];
    }
    return $list;
  }


  public function getAllStore(){
    $all=Store::where("status",1)
              ->column('name','id');
    $result=[
      [
        "value"=>"",
        "label"=>"无",
      ]
    ];

    foreach ($all as $key => $value) {
      $result[]=[
        "value"=>(string)$key,
        "label"=>$value,
      ];
    }

    Api::ok($result);
  }

   public function search_body(){
     $datatime=json_encode([
       date("Y-m-d H:i:s",strtotime("-30 day")),
       date("Y-m-d H:i:s"),
     ]);

     $this->VD([

       "___vue___"=>'
           data(){
             return {
               form:{
                 datetime:'.$datatime.'
               },
               storeList:[]
             }
           },
           mounted(){
             var that=this;
             APP.ajax("getAllStore",{},function(msg){
               if (msg.result){
                 that.storeList=msg.data;
               }else{
                 APP.alert(msg.data);
               }
             });
           }

       '
     ]);
     return '

         <el-form-item  class="col-md-12 col-lg-12" label="产品编码">
           <el-input
             v-model="form.goods"
             placeholder="">
           </el-input>
         </el-form-item>

         <el-form-item  class="col-md-12 col-lg-12" label="库位编码">
           <el-select v-model="form.store" filterable placeholder="请选择">
             <el-option
               v-for="item in storeList"
               :key="item.value"
               :label="item.label"
               :value="item.value">
             </el-option>
           </el-select>
         </el-form-item>

         <el-form-item  class="col-md-12" label="时间范围">
          <c-datetime-picker
            v-model="form.datetime"
            type="datetimerange"
          >
          </c-datetime-picker>
         </el-form-item>


     ';
   }
   public function result_body(){
     $this->VD([
       "___btn___"=>'
          <div style="padding-right: 10px;"><el-button @click="doRecommend(scope.row.id)" type="text" size="small">推荐</el-button></div>
       ',
       "___vue___"=>'

           methods:{
             doRecommend(id){
               APP.ajax("/admin/goods/recommend/set",{id:id},function(msg){
                 if (msg.result){
                   APP.alert(msg.data,true);
                 }else{
                   APP.alert(msg.data);
                 }
               });
             }
           }

       '
     ]);
     return '
     <el-table-column
       fixed="left"
       prop="id"
       label="单号"
       width="60">
     </el-table-column>

     <el-table-column
       fixed="left"
       prop="date"
       label="日期"
       width="180">
     </el-table-column>

     <el-table-column
       fixed="left"
       prop="goods_code"
       label="产品编码"
       width="150">
     </el-table-column>

     <el-table-column
       fixed="left"
       prop="goods_name"
       label="产品名称"
       >
     </el-table-column>


     <el-table-column
       fixed="left"
       prop="store_name"
       label="库位名称"
       >
     </el-table-column>

     <el-table-column
       fixed="left"
       prop="num"
       label="数量"
       align="right"
       width="120">
     </el-table-column>



     ';
   }




 }
